<%@ Page Language="vb" %>

<html>
<body>
<script runat="server" language="vb">
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim filePath As String = Server.MapPath("")

		If Not IsPostBack Then
			Response.Write("<table>")
			Response.Write("<tr><td></td><td colspan=3>Folder Permissions</td><td width=10></td><td colspan=2>File Permissions</td></tr>")
			Response.Write("<tr><td>Folder</td><td>Read</td><td>Create</td><td>Delete</td><td width=10></td><td>Create</td><td>Delete</td></tr>")

			'Test Folder Create Permission on root
			CheckFolderPermissions(filePath)

			ParseSubFolders(filePath)

			Response.Write("</table>")
			Response.End()
		End If
    End Sub

    Private Sub ParseSubFolders(ByVal filePath As String)

        Try
            Dim arrFolders As String() = System.IO.Directory.GetDirectories(filePath)
            Dim strFolder As String

            For Each strFolder In arrFolders
                CheckFolderPermissions(strFolder)

                'Check for further subdirectories
                ParseSubFolders(strFolder)
            Next
        Catch ex As Exception
        End Try

    End Sub

    Private Sub CheckFolderPermissions(ByVal filePath As String)

        Response.Write("<tr>")
        Response.Write("<td>" & filePath & "</td>")
        Response.Write("<td>" & CheckFolderBrowse(filePath, "Ok", "--") & "</td>")
        Response.Write("<td>" & CheckFolderCreate(filePath, "Ok", "--") & "</td>")
        Response.Write("<td>" & CheckFolderDelete(filePath, "Ok", "--") & "</td>")
        Response.Write("<td width=10></td>")
        Response.Write("<td>" & CheckFileCreate(filePath, "Ok", "--") & "</td>")
        Response.Write("<td>" & CheckFileDelete(filePath, "Ok", "--") & "</td>")
        Response.Write("</tr>")

    End Sub

    Private Function CheckFolderBrowse(ByVal strFolder As String, ByVal trueString As String, ByVal falseString As String) As String
        ' test folder browse permission
        Try
            Dim arrFolders As String() = System.IO.Directory.GetDirectories(strFolder)
            Return trueString
        Catch ex As Exception
            'No Folder browse Permission
            Return falseString
        End Try

    End Function

    Private Function CheckFolderCreate(ByVal strFolder As String, ByVal trueString As String, ByVal falseString As String) As String
        ' test folder create permission
        Try
            System.IO.Directory.CreateDirectory(strFolder & "/verify")
            Return trueString
        Catch ex As Exception
            'No Folder create Permission
            Return falseString
        End Try
    End Function

    Private Function CheckFolderDelete(ByVal strFolder As String, ByVal trueString As String, ByVal falseString As String) As String
        ' test folder delete permission
        Try
            If System.IO.Directory.Exists(strFolder & "/verify") Then
                System.IO.Directory.Delete(strFolder & "/verify", False)
                Return trueString
            Else
                Return "NA"
            End If
        Catch ex As Exception
            'No Folder delete Permission
            Return falseString
        End Try
    End Function

    Private Function CheckFileCreate(ByVal strFolder As String, ByVal trueString As String, ByVal falseString As String) As String
        ' test file write permission
        Try
            Dim objStream As System.IO.StreamWriter
            objStream = System.IO.File.CreateText(strFolder & "\verify.txt")
            objStream.WriteLine("")
            objStream.Close()
            Return trueString
        Catch ex As Exception
            'No file write Permission
            Return falseString
        End Try
    End Function

    Private Function CheckFileDelete(ByVal strFolder As String, ByVal trueString As String, ByVal falseString As String) As String
        ' test file delete permission
        Try
            If System.IO.File.Exists(strFolder & "/verify.txt") Then
                System.IO.File.Delete(strFolder & "/verify.txt")
                Return trueString
            Else
                Return "NA"
            End If
        Catch ex As Exception
            'No file delete Permission
            Return falseString
        End Try
    End Function
    
</script>
</body>
</html>
